package com.example.task1.ui.AllService.garbage;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.base.BaseActivity;
import com.example.task1.bean.GarbageSortBean;
import com.example.task1.http.Api;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import retrofit2.Call;

public class GarbageSearchActivity extends BaseActivity {

    private String content;

    private int pageNum = 1;
    private boolean isTrue = true;
    private RecyclerView rvList;
    private GarbageSortAdapter garbageSortAdapter;
    private LinearLayoutManager linearLayoutManager;
    private TextView end;

    private void loadCommentList() {
        rvList.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                if (garbageSortAdapter.getItemCount() - 1 == linearLayoutManager.findLastVisibleItemPosition() && isTrue) {
                    isTrue = false;
                    initData();
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_garbage_search);
        content = getIntent().getStringExtra("content");
        initData();
        initView();
        setAdapter();
        loadCommentList();
    }


    private void setAdapter() {
        garbageSortAdapter = new GarbageSortAdapter(this);
        linearLayoutManager = new LinearLayoutManager(this);
        rvList.setAdapter(garbageSortAdapter);
        rvList.setLayoutManager(linearLayoutManager);
    }

    private void initData() {
        Call<ApiResp> call = Api.api().get(MyApplication.TOKEN, "/prod-api/api/garbage-classification/garbage-classification/list?pageNum=" + pageNum + "&pageSize=100&name=" + content);
        Api.send(call, GarbageSortBean.class, new CallBack<GarbageSortBean>() {
            @Override
            public void success(GarbageSortBean data) {
                if (garbageSortAdapter.getData().size() < data.getTotal()) {
                    isTrue = true;
                    garbageSortAdapter.setData(data.getRows());
                    pageNum++;
                    end.setVisibility(View.GONE);

                } else {
                    MyApplication.show("没有更多数据了");
                    end.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void error(String msg) {

            }
        });
    }


    @Override
    public String getMyTitle() {
        return "搜索";
    }

    private void initView() {
        end = findViewById(R.id.end);
        rvList = findViewById(R.id.rv_list);
    }
}